package com.edu.app.controller.core;

import com.edu.app.bean.core.SysContact;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.core.SysContactService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/contacts")
@RestController
public class SysContactController {

    @Autowired
    private SysContactService sysContactService;



    @RequestMapping(value = "")
    @RequiresAuthentication
    public ModelMap getContactList (){

        try{
            List<SysContact> contactList = sysContactService.getObjectList();
            return MsgUtil.retMsg(SysMessage.GET_CONTACT_SUCCEED, StaticConstant.CONTACT_LIST, contactList);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping(value = "/{id}")
    @RequiresAuthentication
    public ModelMap getContactById(@PathVariable Integer id){

        try{
            SysContact sysContact  = sysContactService.getObjectById(id);
            return MsgUtil.retMsg(SysMessage.GET_CONTACT_SUCCEED, StaticConstant.CONTACT, sysContact);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping("/pages")
    @RequiresAuthentication
    public ModelMap getContactByPage(@RequestParam Integer offset, @RequestParam Integer limit) {

        try {
            PageInfo pageInfo = sysContactService.findItemByPage(offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_BY_PAGE_SUCCEED, "page_info", pageInfo);
        } catch (Exception e) {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    @PostMapping(value = "")
    @RequiresAuthentication
    public ModelMap addContact(@RequestHeader("Authorization") String token,
                                @RequestBody SysContact sysContact,
                                @RequestParam Integer accountId){

        try{
            String username = JWTUtil.getUsername(token);
            sysContact.initializeSystemColumns(username);

            Integer r = sysContactService.add(sysContact,accountId);

            if(r > 0){
                //List<SelectedUsers> selectedUsers = JSON.parseArray(attendeeList, SelectedUsers.class);
                //Assign activity to corresponding users
                //for(int i=0; i<selectedUsers.size(); i++){
                //    sysUserActivityService.add(new SysUserActivity(selectedUsers.get(i).getValue(), sysContact.getId()));
                //}
                return MsgUtil.retMsg(SysMessage.ADD_CONTACT_SUCCEED, null, null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.CONTACT_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @PutMapping(value = "")
    @RequiresAuthentication
    public ModelMap update (@RequestHeader("Authorization") String token, @RequestBody SysContact sysContact){

        String username = JWTUtil.getUsername(token);
        sysContact.initializeSystemColumns(username);

        Integer i = sysContactService.update(sysContact);

        if(i > 0 ){
            return MsgUtil.retMsg(SysMessage.UPDATE_CONTACT_SUCCEED, null, null);
        }
        else{
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }

    @DeleteMapping(value = "")
    @RequiresAuthentication
    public ModelMap delete (@RequestParam Integer id){

        ModelMap map = new ModelMap();
        Integer i = sysContactService.delete(id);
        if(i > 0 )
        {
            //continue to delete the intersection table related with current activity

            return MsgUtil.retMsg(SysMessage.DELETE_CONTACT_SUCCEED, null, null);
        }
        else
        {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }
}
